
//
//  ChatTableViewCell.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/29.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "ChatTableViewCell.h"

#define EdgeInsetsTop 30

@interface ChatTableViewCell ()

@property (nonatomic, strong) UIImageView * headImageView; // 头像
@property (nonatomic, strong) UILabel * nameLabel; // 昵称
@property (nonatomic, strong) UILabel * dateLabel; // 时间
@property (nonatomic, assign) CGFloat cellHeight; // cell高度

@end

@implementation ChatTableViewCell

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 去除选中状态
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 添加视图
        [self addSubview:self.headImageView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.messageButton];
        [self addSubview:self.dateLabel];
        
        // 自动布局
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headImageView);
            make.top.equalTo(_headImageView.mas_bottom);
        }];
        [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_messageButton).offset(-20);
            make.bottom.equalTo(_messageButton).offset(-10);
        }];
    }
    return self;
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (CGFloat)getCellHeight {
    return _cellHeight + EdgeInsetsTop * 2;
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (void)setCellData:(NSDictionary *)cellData {
    // 单元格赋值
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(cellData[@"friendHead"])] placeholderImage:[UIImage imageNamed:@"default_head_s.png"]];
    _nameLabel.text = cellData[@"friendName"];
    _messageButton.messageLabel.text = cellData[@"msg"];
    [_messageButton setBackgroundImage:[UIImage imageNamed:([cellData[@"msgTo"] isEqualToString:[LoginUserInfo defaultUserInfo].userid] ? @"i_学有消息_对话框_03.png" : @"i_学友消息_对话框2_03.png")] forState:UIControlStateNormal];
    _dateLabel.text = cellData[@"postTime"];
    
    NSString * message = cellData[@"msg"];
    // 设置消息视图最大尺寸
    CGSize maxSize = CGSizeMake(200, MAXFLOAT);
    // 计算当前消息字体显示需要的尺寸
    CGSize textSzie = [message textSizeWithFont:[UIFont systemFontOfSize:20] maxSize:maxSize];
    // 消息框高度
    CGFloat messageHeight = textSzie.height;
    _cellHeight = messageHeight > SCREEN_HEIGHT * 0.1 ? messageHeight + 2 * EdgeInsetsTop : SCREEN_HEIGHT * 0.1;
    
    // 自动布局
    if (![cellData[@"msgTo"] isEqualToString:[LoginUserInfo defaultUserInfo].userid]) {
        [_dateLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_messageButton).offset(-40);
        }];
    }
    [_headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(self.mas_width).multipliedBy(0.08);
        make.top.offset(SCREEN_HEIGHT * 0.02);
        if ([cellData[@"msgTo"] isEqualToString:[LoginUserInfo defaultUserInfo].userid]) {
            make.centerX.equalTo(self).multipliedBy(0.2);
        } else {
            make.centerX.equalTo(self).multipliedBy(1.8);
        }
    }];
    [_messageButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self).multipliedBy(0.73);
        make.height.offset(_cellHeight);
        make.top.equalTo(_headImageView);
        if ([cellData[@"msgTo"] isEqualToString:[LoginUserInfo defaultUserInfo].userid]) {
            make.left.equalTo(_headImageView.mas_right);
        } else {
            make.right.equalTo(_headImageView.mas_left);
        }
    }];
    
}
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = RGB_COLOR(121, 121, 121, 1);
        _nameLabel.font = [UIFont systemFontOfSize:20];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}
- (MessageButton *)messageButton {
    if (!_messageButton) {
        _messageButton = [[MessageButton alloc] init];
        _messageButton.titleLabel.font = [UIFont systemFontOfSize:20];
        _messageButton.adjustsImageWhenHighlighted = NO;
        [_messageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _messageButton;
}
- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.textColor = RGB_COLOR(121, 121, 121, 1);
    }
    return _dateLabel;
}

@end
